/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.harmonyos.studyharmonyos.helper;

import com.harmonyos.studyharmonyos.ResourceTable;
import com.harmonyos.studyharmonyos.bean.DeviceData;
import com.harmonyos.studyharmonyos.helper.listcomponent.CommentViewHolder;
import com.harmonyos.studyharmonyos.helper.listcomponent.ListComponentAdapter;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.distributedschedule.interwork.DeviceInfo;
import ohos.distributedschedule.interwork.DeviceManager;

import java.util.ArrayList;
import java.util.List;

import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_CONTENT;
import static ohos.agp.components.ComponentContainer.LayoutConfig.MATCH_PARENT;

/**
 * DeviceSelectDialog
 *
 * @since 2021-02-04
 */
public class DeviceSelectDialog extends CommonDialog {
    private static final int CORNER_RADIUS = 10;
    ListContainer mListContainer;
    List<DeviceData> mDeviceList = new ArrayList<>();
    ListComponentAdapter listComponentAdapter;
    private DeviceInfo mCheckedDevice;

    /* 确定按钮点击回调接口*/
    public interface OnclickListener {
        void onYesClick(DeviceInfo deviceInfo);
    }

    /* 赋值回调*/
    public void setListener(OnclickListener listener) {
        mOnclickListener = listener;
    }

    private Context mContext;
    private OnclickListener mOnclickListener;

    /**
     * DeviceSelectDialog
     *
     * @param context context
     */
    public DeviceSelectDialog(Context context) {
        super(context);
        this.mContext = context;
    }

    /*创建设备选择对话框*/
    @Override
    protected void onCreate() {
        super.onCreate();
        Component rootView = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_dialog_layout_device, null, false);//声明弹窗布局
        mListContainer = (ListContainer) rootView.findComponentById(ResourceTable.Id_list_container_device);//声明设备列表布局
        List<DeviceInfo> deviceInfoList = DeviceManager.getDeviceList(DeviceInfo.FLAG_GET_ONLINE_DEVICE);//获取当前网络在线设备列表
        mDeviceList.clear();//清空设备列表
        for (int i = 0; i < deviceInfoList.size(); i++) {
            mDeviceList.add(new DeviceData(i <= 0, deviceInfoList.get(i)));//构造数据列表，默认第一个设备选中
        }
        if (deviceInfoList.size() > 0) {
            mCheckedDevice = deviceInfoList.get(0);//默认选中第一个设备
        }
        setItemProvider();//构建显示设备列表
        Text operateYes = (Text) rootView.findComponentById(ResourceTable.Id_operate_yes);
        operateYes.setClickedListener(//确定按钮点击监听
                component -> {
                    if (mOnclickListener != null && mCheckedDevice != null) {//如果回调函数已赋值，并且有选中设备、则回调函数并传递该设备信息
                        mOnclickListener.onYesClick(mCheckedDevice);
                    }
                });
        Text operateNo = (Text) rootView.findComponentById(ResourceTable.Id_operate_no);
        operateNo.setClickedListener(//取消按钮点击监听
                component -> {
                    hide();//隐藏弹窗
                });
        setSize(MATCH_PARENT, MATCH_CONTENT);//设置对话框的大小
        setAlignment(LayoutAlignment.CENTER);//设置对话框居下对齐
        setCornerRadius(CORNER_RADIUS);//设置对话框圆角的半径
        setAutoClosable(true);//设置启用自动关闭对话框
        setContentCustomComponent(rootView);//设置对话框根局部
        setTransparent(true);//设置为对话框启用透明背景。
    }

    /**
     * 构建扫描显示适配器
     */
    private void setItemProvider() {
        listComponentAdapter =
                new ListComponentAdapter<DeviceData>(mContext, mDeviceList, ResourceTable.Layout_dialog_device_item) {
                    @Override
                    public void onBindViewHolder(CommentViewHolder commonViewHolder, DeviceData item, int position) {
                        commonViewHolder.getTextView(ResourceTable.Id_item_desc)
                                .setText(item.getDeviceInfo().getDeviceName());
                        //判断设备类型
                        switch (item.getDeviceInfo().getDeviceType()) {
                            case SMART_PHONE://手机
                                commonViewHolder.getImageView(ResourceTable.Id_item_type)
                                        .setPixelMap(ResourceTable.Media_dv_phone);
                                break;
                            case SMART_PAD://平板
                                commonViewHolder.getImageView(ResourceTable.Id_item_type)
                                        .setPixelMap(ResourceTable.Media_dv_pad);
                                break;
                            case SMART_WATCH://手表
                                commonViewHolder.getImageView(ResourceTable.Id_item_type)
                                        .setPixelMap(ResourceTable.Media_dv_watch);
                                break;
                            default:
                        }
                        commonViewHolder
                                .getImageView(ResourceTable.Id_item_check)
                                .setPixelMap(
                                        item.isChecked()
                                                ? ResourceTable.Media_checked_point
                                                : ResourceTable.Media_uncheck_point);
                    }

                    @Override
                    public void onItemClick(Component component, DeviceData item, int position) {
                        super.onItemClick(component, item, position);
                        for (DeviceData mDevice : mDeviceList) {
                            mDevice.setChecked(false);
                        }
                        mDeviceList.get(position).setChecked(true);
                        listComponentAdapter.notifyDataChanged();
                        mCheckedDevice = item.getDeviceInfo();
                    }
                };
        mListContainer.setItemProvider(listComponentAdapter);
    }
}